/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simulation;

import jWASA.PHR;
import java.sql.Date;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Random;
import samples.*;

/**
 *
 * @author gianmariospacagna
 */
public class SamplesGenerator {

    public void simulate(SimulationPhantomPhobia simulation, ElaboratedSampleCollection outputSamples, Collection<PHR> outputPHRs) {


        Collection<SampleLight> lightSamples = outputSamples.getLight();

        Collection<SampleHeartBeat> heartbeatSamples = outputSamples.getHeartbeat();



        long initTimestamp = simulation.startDate.getTime();
        long hbTimestamp = initTimestamp + (simulation.fallAsleep * 60 * 1000);
        long lightTimestamp = initTimestamp;
        for (int month = simulation.startDate.getMonth(); month < simulation.endDate.getMonth(); month++) {
            Boolean scoobyDoo = false;
            Boolean scoobyDooNight = false;
            for (int day = 0; day < 28; day++) {

                Date currentDate = new Date(2011, month, day);
                //generate PHR
                if (scoobyDooNight) {
                    scoobyDooNight = false;
                }
                if (scoobyDoo) {
                    scoobyDoo = false;
                    scoobyDooNight = true;
                }
                if (day % simulation.nScoobyDoo == 0) {
                    PHR phr = new PHR("shaggy", new Date(2011, month, day), new Time(16, 30, 0), "Mistery Home", "Very fine", "Shaggy watched ScoobyDoo on TV", new HashSet<String>());
                    outputPHRs.add(phr);
                    scoobyDoo = true;
                    //System.out.println("PHR-> "+phr.toString());
                } else {
                    PHR phr = new PHR("shaggy", new Date(2011, month, day), new Time(16, 30, 0), "Mistery Home", "Very fine", "Shaggy watched The Smurfs on TV", new HashSet<String>());
                    outputPHRs.add(phr);

                }




                //generate heartbeat
                MonthLightHours mlh = simulation.monthsLightHours.get(month);

                hbTimestamp = currentDate.getTime() + (simulation.fallAsleep * 60 * 1000);
                boolean agitated = false;
                //System.out.println("Day: " + currentDate + " Is the night of ScoobyDoo? " + scoobyDooNight);
                for (double min = simulation.fallAsleep; min < simulation.wakeUp;) {
                    SampleHeartBeat hbSample;
                    if ((min >= simulation.agitationStart & min < simulation.agitationEnd)
                            & (scoobyDooNight)
                            & ((isDark(hbTimestamp, mlh) & (new Random().nextInt(100) < simulation.probAgitationGivenDark))
                            | (!isDark(hbTimestamp, mlh) & (new Random().nextInt(100) < simulation.probAgitationGivenNotDark)))) {
                        hbSample = new SampleHeartBeat(hbTimestamp, 0, 0, 0, 0, simulation.maxHBrate);


                    } else {
                        hbSample = new SampleHeartBeat(hbTimestamp, 0, 0, 0, 0, simulation.regularHBrate);
                    }

                    heartbeatSamples.add(hbSample);
                    //System.out.println("HBSample-> " + hbSample.toString()+ " min: "+min);
                    min += ((double) (simulation.periodHBMeasurements) / 60000);
                    hbTimestamp += simulation.periodHBMeasurements;
                }

//generate light
                lightTimestamp = currentDate.getTime();
                for (int min = 0; min < 1439; min += 30) {

                    SampleLight lightSample;
                    if (min < mlh.sunrise | min > mlh.sunset) {
                        lightSample = new SampleLight(lightTimestamp, 5);
                    } else {
                        lightSample = new SampleLight((lightTimestamp), 80);
                    }
                    lightSamples.add(lightSample);
                    //System.out.println("Light-> "+lightSample.toString());
                    lightTimestamp += 30 * 60 * 1000;
                }
            }
        }
    }

    public void print(SimulationPhantomPhobia simulation, ElaboratedSampleCollection outputSamples, Collection<PHR> outputPHRs) {
        System.out.println("PHR Samples");

        for (PHR phr : outputPHRs) {
            System.out.println("PHR-> " + phr.toString());
        }
        System.out.println("HeartBeat Samples");

        for (SampleHeartBeat shb : outputSamples.getHeartbeat()) {
            System.out.println("HeartBeat-> " + shb.toString());
        }

        System.out.println("Light Samples");

        for (SampleLight sl : outputSamples.getLight()) {
            System.out.println("HeartBeat-> " + sl.toString());
        }

    }

    private boolean isDark(long hbTimestamp, MonthLightHours mlh) {
        Time day = new Time(hbTimestamp);

        int hours = day.getHours();
        int min = day.getMinutes();
        min += (60 * hours);

        if (min < mlh.sunrise | min > mlh.sunset) {
            return true;
        } else {

            return false;
        }

    }
}
